/* R. J. Cano, Oct 25 2016, 6:00 GMT

    Formerly: << Thinking contemplation since (approx. Nov 2011, Jan 12) years ago, finally
                  crystallized in the form of the present INFORMAL report on: Oct 13..23, 2016 >>

    Currently: The same informal report reviewed without involving the huge and boring auxiliary script.

----------------------------------------------------------------------------------------------------------------

 ABSTRACT: Essentially what will be done below is to use the output obtained from the execution of the Steinhaus-Jhonson-Trotter

 ( If you wish, you could review it at: https://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm )

 generating the first n!-1 differences among permutations for n letters assumed to be the integer numbers in 0..n-1;

 Then by observing these described differences seem to behave a very curious and intriguing pattern, where factors consisting of
 powers of "10" are involved, we proceed to analyze separately the distinguishable parts of the obtained terms: Simple integer
 numbers and powers of "10".

 At the time of this submission to OEIS Wiki, they were observed symmetric, anti-symmetric sequences or their modifications,
 recurrence (recursive) patterns, and plausible auto-similarity.

 ----------------------------------------------------------------------------------------------------------------

 [ NOTE: The mentioned report is self-content in the present script source code, so there are no executable demos,
         leaving it free for the reader the freedom of performing experiments by himself/herself; ]
*/

/*The following routine implements recursively the Steinhaus-Jhonson-Trotter algorithm trough a sieve based on modular arithmetic. Usage: Initial "d"iagonal: 0 "/" (default) or 1 "\"; "w"aving diagonals? 1 "yes" (default), or 0 "no"; Output "format" 1 "A vector of vectors" (default), or 0 "A huge vector" */
permuteSJT_1(n,d=0,w=1,format=1)={
  if(n<1,return([]));
  if(n==1,return([0]));
  if(n==2,if(d,if(format,return([[1,0],[0,1]]),return([1,0,0,1])),if(format,return([[0,1],[1,0]]),return([0,1,1,0])))); /* This line of code suffered a correction! */
  my(u=permuteSJT_1(n-1,d,w,0)); /* Internally we use only the output format zero. */
  my(c=1,sqr_n=n*n);
  d=!d;
  my(v=vector(n!*n,i,0*if(w&&!((i-1)%sqr_n),d=!d)+(n-1)*(!d*(((i%sqr_n)%(n-1)==1)&&(1!=(i%sqr_n)))+!!d*((!(i%sqr_n))||(1==((i%sqr_n)%n-(i%sqr_n)\n))))));
  for(k=0,(n-1)!-1,
    for(j=1,n,
      for(i=1,n-1,
        if(c<=#v,
          while(v[c],c++);
          v[c]=u[k*(n-1)+i];
          c++
        )
      )
    )
  );
  c*=0;
  if(format,return(vector(n!,y,vector(n,x,v[c++]))),return(v))
} /* Note(s): 1) Here the usage of divrem() was avoided intentionally. 2) This is an experimental implementation: Probably slower than other choices. */

/*
 *     ----------------------------------------------------------------------------------------
 *       Analyzing the 1st differences "divided by 9", for consecutive elements in the output
 *     ----------------------------------------------------------------------------------------
*/

y(n)={
  n=min(9,!n+n*!!n); /* C'est la vie! (Life is not fair!) */
  my(B=max((n>2)*10,n),a);
  a=permuteSJT_1(n);
  a=vector(-1+#a,i,-a[i]+a[i+1]);
  a=vector(#a,i,subst(Pol(a[i],UnknowN),UnknowN,B)/(B-1));
  return(a)
}

y4=[1,20,300,1,-300,-10,-2,200,2,30,100,1,-100,-20,-3,-100,3,10,200,-2,-200,-30,-1]; /* Evaluation result for: y(4) */

/* Example. When properly formatted, this (y4) should look like:

     y4= [
          +001,+020,+300,
                         +001,
          -300,-010,-002,
                         +200,
          +002,+030,+100,
                         +001,
          -100,-020,-003,
                         -100,
          +003,+010,+200,
                         -002,
          -200,-030,-001
         ];

  The items observed in such scheme, might be count by taking advantage from the following decomposition:

    n!-1= (n-1)!*n-1= (n-1)!*(n-1)+(n-1)!-1;

  The last column at the right side has (n-1)!-1 elements. By borrowing the "e" notation for real numbers,
  we can re-format it into:

     y4= [
          +1e0,+2e1,+3e2,
                         +1e0,
          -3e2,-1e1,-2e0,
                         +2e2,
          +2e0,+3e1,+1e2,
                         +1e2,
          -1e2,-2e1,-3e0,
                         -1e2,
          +3e0,+1e1,+2e2,
                         -2e0,
          -2e2,-3e1,-1e0
         ];

  Which turns out the possible interesting properties such kind of construction has when the output for the
  execution of the SJT algorithm is used instead of the lexicographic increasing / decreasing sort assumed
  in A215940;

  Moreover, although everything except the last column looks easy to explain, it could be interesting: Try to
  give a precise description for the last column at right, in terms of patterns, properties, and (why NOT?)
  additional algorithms.

  The easy part of the description (easy as it was mentioned) corresponding to the (n-1)!*(n-1) matrix at
  left, is: Bases are signed integers taken from the cyclic half of permutations for the letters 1..n-1,

  A brief illustration would suffice:

  If n=4, then the previous description means n-1=3, the letters will be 1..3, all permutations are:
  (recursively)

  1;
  12, 21;
  123, 132, 312, 321, 231, 213;

  Well, if they are tagged as "0" for cyclic, and "x" for anti-cyclic, the corresponding pattern is:
  0, x, 0, x, 0, x;

  Since we are interested in the cyclic half, then we pick from there only the zeros,which give us:

  123, 312, 231;

  And by looking again inside the (n-1)!*(n-1) matrix at the left side of y4, these 3 permutations
  are repeated twice with their digits distributed as the signed bases, and by keeping their
  sequence: [ [1,2,3], [3,1,2], [2,3,1] ]; Such is the cyclic (or even) half of the SJT permutations
  for this particular case.

  In other hand, the exponents for such matrix are related to the first and the last permutations in
  lexicographic increasing order (the output obtained with the Narayana Pandita's algorithm) for
  the n-1 letters letters starting with zero: [[0,1,2], [2,1,0]]; These two permutations only,
  in a similar way, distributed and repeated among the exponents.

  Now, let us pick among the n!-1 elements from the output for y(n) those y(n)[j] such that j is 0 mod n;

*/

x(n)={
  my(L:list,a=y(n),j=1);
  L=List();
  while(j++<#a,
    if(!(j%n),listput(L,a[j]));
  );
  return(vector(#L,i,L[i]))
}

/* And we could compare y(n) against x(n+1); ...for example for n=4, we have:

     y(4) <--- [1,  20, 300,  1, -300,  -10, -2,  200, 2,  30, 100,  1, -100,  -20, -3,  -100, 3,  10, 200,  -2, -200,  -30, -1]
     x(5) <--- [1, 200, 300, 10, -300, -100, -2, 2000, 2, 300, 100, 10, -100, -200, -3, -1000, 3, 100, 200, -20, -200, -300, -1]

   And they clearly differ!, but they differ in a very curious way: for 1<=j<=n!-1 y(n)[j] differ from x(n+1)[j] only for even j,
   this is, if j is 0 mod 2;

   Let's call "checking01" to such hypothesis.
*/

checking01(n)={my(a=y(n),b=x(n+1));prod(j=1,(n!-1)\2,a[2*j+1]==b[2*j+1])}

/*

 prod(k=1,8,checking01(k)) returns 1; it works!, the proposed hypothesis explains correctly the observed pattern.

   ---------------------------------
      ? prod(k=1,8,checking01(k))
      time = 1min, 15,219 ms.
      1
   ---------------------------------

 Moreover, evidently, without the zeros, by removing every zero from y(n) and x(n+1), the results are identical.

 THIS IS, by studying separately bases and exponents, it is found that the output for y(n) partially depends on
 the whole output for y(n-1);

*/

checking02(n)={my(a=y(n),b=x(n+1));prod(j=1,n!-1,10^!(j%2)*a[j]==b[j])}

/*

 A more general and precise hypothesis can be now tested: y(n) and x(n+1) match if every element at an
 even place inside y(n) is multiplied by 10;

 prod(k=1,8,checking02(k))

   ---------------------------------
      ?  prod(k=1,8,checking02(k))
      time = 1min, 15,166 ms.
      1
   ---------------------------------

 This is very a curious finding. The first differences themselves partially are part of a recursive
 (and possibly auto-similar) pattern.

 The constraint applied here about k ranging in 1..8 is due execution environment + implementation
 limitations, but when properly implemented, both hypotheses should be the correct explanations of
 the observed facts for any k in 1..U with any U>0;

 If we were interested in removing the every zero from a vector of integer numbers: */

z0(t)={while(!(t%10),t/=10);return(t)} /* "t" should be an integer number */
z(v)=vector(#v,i,z0(v[i])); /* "v" should be a vector */

/*

 In such sense, the comparison for the hypothetical connection between y(n) and x(n+1) could be
 done now as: z(y(n))==z(x(n+1))

 And for example we become enabled to have results like this:

   ---------------------------------
      ? prod(n=1,8,z(y(n))==z(x(n+1)))
      time = 1min, 19,169 ms.
      1
   ---------------------------------

 But more interesting and important, by having into account the aim of determining if the first
 differences for the output of execution for the SJT algorithm also behaves auto-similarity in
 analogous way to A217626, the routine z() as defined here enables the search of z(y(n)) inside
 z(y(n+q)) for q>1;

 The presence of the unit inside z(y(j)) for j in 2..9 has counters:
 1, 3, 9, 33, 153, 873, 5913, 46233; Notice the 1st differences of
 this counting sequence are the first factorials from 2! to 8!;

 In other hand, at least z(y(3)) is present once inside z(y(5)), also it is present more than
 twice inside z(y(7)) and z(y(9)); By comparison inclusively against itself, z(y(3)) is present
 respectively: 1, 1, 12, 360 times in z(y(k)) for k=3,5,7,9;

 But beyond of (what might be merely) some coincidences, no more coincidences of this kind
 were detected.

*/

/*EOF*/